﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using imaginecup.util;
using Imaginecup.shareData;
using Imaginecup.sprite.particle;

namespace Imaginecup.sprite
{

    public class  FadeSprite:ISprite
    {
        private Texture2D m_BlackImage = null;
        private IColorClass m_ColorClass;
        private Color m_Color = Color.Black;

        private void CreateTexture(GraphicsDevice device)
        {
            m_BlackImage = new Texture2D(device, 1, 1, false, SurfaceFormat.Color);
            Color[] pixels = new Color[1];
            pixels[0] = m_Color;
            m_BlackImage.SetData<Color>(pixels);
        }
        
        public void SetFadeColor(Color fadeColor)
        {
            m_Color = fadeColor;
        }
        public void SetColorClass(IColorClass colorClass)
        {
            m_ColorClass = colorClass;
        }


        public void Draw(SpriteBatch spriteBatch, EntityInfo entityInfo)
        {
            if (m_BlackImage == null) // 텍스쳐가 없다면, 만들고 그린다.
                CreateTexture(GraphicShareData.GetShareData().GetGraphicDevice());

            spriteBatch.Begin();
            spriteBatch.Draw(m_BlackImage, new Rectangle((int)entityInfo.mPosition.X, (int)entityInfo.mPosition.Y, (int)entityInfo.mSize.X, (int)entityInfo.mSize.Y),
                m_ColorClass.GetColor());

            spriteBatch.End();
        }

        public void Release()
        {
            m_BlackImage = null;
        }
    }
}
//static public class cFadeRenderer
//    {
//        public enum RenderMode { FADE_IN = 0, FADE_OUT };

//        static Texture2D m_BlackImage = null;
//        static RenderMode m_Mode = RenderMode.FADE_IN;

//        static double m_StartTick = 0;
//        static double m_DestDuration = 0;

//        static public void SetFadeMode(RenderMode mode)
//        {
//            m_Mode = mode;
//        }

//        static private void CreateTexture(GraphicsDevice device) 
//        {
//            m_BlackImage = new Texture2D(device, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
//            Color[] pixels = new Color[1];
//            pixels[0] = Color.Black;
//            m_BlackImage.SetData<Color>(pixels);
//        }

//        static public void SetTime(GameTime time, int duration_ms)
//        {
//            m_DestDuration = duration_ms;
//            m_StartTick = time.TotalGameTime.TotalMilliseconds;
//        }

//        static public void Draw(GraphicsDevice device, SpriteBatch spriteBatch, GameTime time)
//        {
//            // 몇 ms 지났는지 계산.
//            double currentDuration = time.TotalGameTime.TotalMilliseconds - m_StartTick;

//            int alphaValue = 0;

//            if (m_Mode == RenderMode.FADE_IN)
//            {
//                alphaValue = 255 - (int)((float)(currentDuration / m_DestDuration) * 255.0f);

//                // byte는 0~255까지 허용. 최소값 체크
//                if (alphaValue < 0)
//                    alphaValue = 0;
//            }

//            else if (m_Mode == RenderMode.FADE_OUT)
//            {
//                alphaValue = (int)((float)(currentDuration / m_DestDuration) * 255.0f);

//                // byte는 0~255까지 허용. 최대값 체크
//                if (alphaValue > 255)
//                    alphaValue = 255;
//            }
            
//            if (m_BlackImage == null) // 텍스쳐가 없다면, 만들고 그린다.
//                CreateTexture(device);

//            spriteBatch.Draw(m_BlackImage, device.Viewport.TitleSafeArea, new Color(Color.White, (byte)alphaValue));
//        }

//    }

//    public class Game1 : Microsoft.Xna.Framework.Game
//    {
//        GraphicsDeviceManager graphics;
//        SpriteBatch spriteBatch;

//        Texture2D m_BackImage;
        
//        public Game1()
//        {
//            graphics = new GraphicsDeviceManager(this);
//            Content.RootDirectory = "Content";
//        }

//        protected override void Initialize()
//        {
//            base.Initialize();
//        }

//        protected override void LoadContent()
//        {
//            spriteBatch = new SpriteBatch(GraphicsDevice);

//            m_BackImage = Content.Load<Texture2D>("소녀시대");
//        }

//        protected override void UnloadContent()
//        {

//        }

//        protected override void Update(GameTime gameTime)
//        {
//            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
//                this.Exit();

//            const int fadeDuation_ms = 1000; // 1초

//            if (Keyboard.GetState().IsKeyDown(Keys.A)) // fade in
//            {
//                cFadeRenderer.SetTime(gameTime, fadeDuation_ms);
//                cFadeRenderer.SetFadeMode(cFadeRenderer.RenderMode.FADE_IN);
//            }
//            else if (Keyboard.GetState().IsKeyDown(Keys.S)) // fade out
//            {
//                cFadeRenderer.SetTime(gameTime, fadeDuation_ms);
//                cFadeRenderer.SetFadeMode(cFadeRenderer.RenderMode.FADE_OUT);
//            }

//            base.Update(gameTime);
//        }

//        protected override void Draw(GameTime gameTime)
//        {
//            GraphicsDevice.Clear(Color.CornflowerBlue);

//            spriteBatch.Begin();
//            spriteBatch.Draw(m_BackImage, GraphicsDevice.Viewport.TitleSafeArea, Color.White);

//            cFadeRenderer.Draw(GraphicsDevice, spriteBatch, gameTime);

//            spriteBatch.End();

//            base.Draw(gameTime);
//        }